package org.easiermock.invocationhandlers;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * An InvocationHandler that does nothing and returns a suitable default value 
 * for the method's return type (if it is not void).
 * 
 * @author Neil Fuller
 */
public class DoNothingInvocationHandler implements InvocationHandler {

	private final static Map<Class<?>, Object> DEFAULT_VALUES = new HashMap<Class<?>, Object>();
	static {
		DEFAULT_VALUES.put(Boolean.TYPE, Boolean.valueOf(false));
		DEFAULT_VALUES.put(Byte.TYPE, Byte.valueOf((byte)0));
		DEFAULT_VALUES.put(Short.TYPE, Short.valueOf((short)0));
		DEFAULT_VALUES.put(Integer.TYPE, Integer.valueOf(0));
		DEFAULT_VALUES.put(Long.TYPE, Long.valueOf(0));
		DEFAULT_VALUES.put(Float.TYPE, Float.valueOf(0.0f));
		DEFAULT_VALUES.put(Double.TYPE, Double.valueOf(0.0));
		DEFAULT_VALUES.put(Character.TYPE, Character.valueOf((char)0));
	}
	
	public static final DoNothingInvocationHandler INSTANCE = new DoNothingInvocationHandler();
	
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {

		Class<?> returnType = method.getReturnType();
		return DEFAULT_VALUES.get(returnType);
	}

}
